<?php
/**
 * Category.php
 *
 * @category   Madoqua
 * @package    Module
 * @subpackage Blog
 */

/** base class */
require_once 'Madoqua/Db/Table/Abstract.php';

/** many to many with Blog_Post */
require_once 'PostCategory.php';

/** row class */
require_once 'Category/Row.php';

/**
 * Blog_Category
 *
 * categories for posts
 *
 * @category   Madoqua
 * @package    Module
 * @subpackage Blog
 */
class Blog_Category extends Madoqua_Db_Table_Abstract {

    /**
     * row class
     *
     * @var string
     */
    protected $_rowClass = 'Blog_Category_Row';
    
    /**
     * table name
     *
     * @var string
     */
    protected $_name = 'category';
    
    /**
     * Primary column/columns
     *
     * @var string
     */
    protected $_primary = 'id';
    
   /**
     * tables that depend on this one
     *
     * @var array
     */
    protected $_dependentTables = array('Blog_PostCategory');    

    /**
     * generate a slug for a given string
     *
     * @param string $string
     * @param bool $checkExists check for existing slug and add numbers to slug
     * @return string
     */
    public static function slugify($string, $checkExists = false) {

        $string = Blog_Url::slugify($string);

        if ($checkExists) {
            $model = new Blog_Category();
            $slug = $string;
            $select = $model->select()->where('slug = ?', $slug);
            $x = 2;
            while($model->fetchRow($select) != null) {
                $slug = $string . $x++;
                $select = $model->select()->where('slug = ?', $slug);
            }
            //update if it already exists
            $string = $slug;
        }
        
        return $string;
    }
    
    /**
     * Inserts a new row.
     *
     * @param  array  $data  Column-value pairs.
     * @return mixed         The primary key of the row inserted.
     */
    public function insert(array $data)
    {
        if (empty($data['slug'])) {
            $data['slug'] = Blog_Category::slugify($data['title'], true);
        }
        //update slug if it doesn't exist
        
        return parent::insert($data);
    }
    /**
     * fetch a category by id
     *
     * @param string $id
     * @return Zend_Db_Table_Row
     */
    public function fetchById($id)
    {
        return $this->fetchRow($this->select()->where('id = ?', $id));
    }
    
    /**
     * get a multidimensional array list of categories
     */
    public function getList()
    {
        $categories = $this->fetchAll()->toArray();
        //all categories (not in tree)
        $count = count($categories);
        //number of categories
        
        foreach($categories as $category) {
            if ($category['parent'] != 0) {
                foreach($categories as $key => $try) {
                    if ($try['id'] == $category['parent']) {
                        $categories[$key]['children'][] = $category;
                    }
                }
            }
        }
        foreach($categories as $key => $category) {
            if ($category['parent'] != 0) {
                unset($categories[$key]);
            }
        }
        //this would have been prettier with recursion
        //but I wrote this function after a very long day in a noisy train...
        //sorry :(
        
        return $categories;
    }
    
    
}